home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 948 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. From: "Wil Evers" <wil@ittpub.nl>
  2. Message-ID: <009A04DA6A831C40.49800EAC@ittpub.nl>
  3. X-Original-Date: Wed,  3 Apr 96 11:37:34 WET
  4. Path: in2.uu.net!bounce-back
  5. Date: 03 Apr 96 14:38:46 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. Subject: Re: sample auto_ptr template
  10. X-Vms-Mail-To: IN%"std-c++@ncar.ucar.edu"
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMWKNiuEDnX0m9pzZAQGJdQF9HLlV54hOVi3yapAjSoHOjFsU+YoQ7cqu
  13.     OzcIFKBlG37l4oDXyA1/913snLfNBKpY
  14.     =4Cxs
  15.  
  16. Hi,
  17.  
  18. In article <gregorDp23ts.Axt@netcom.com> gregor@netcom.com (Greg Colvin)
  19. writes:
  20.  
  21. > In Santa Cruz we decided to change the auto_ptr copy semantics to
  22. > allow returns of auto_ptr from functions.  Following is a simple
  23. > implementation.  
  24. >
  25. > [snip]
  26. > template<class X>
  27. >    class auto_ptr {
  28. >       mutable bool owner;
  29. >       X* px;
  30. >       template<class Y> friend class auto_ptr;
  31. >    public:
  32. >       explicit auto_ptr(X* p=0) 
  33. >          : owner(p), px(p) {}
  34. >       template<class Y>
  35. >          auto_ptr(const auto_ptr<Y>& r) 
  36. >             : owner(r.owner), px(r.release()) {}
  37. >       template<class Y>
  38. >          auto_ptr& operator=(const auto_ptr<Y>& r) {
  39. >             if ((void*)&r != (void*)this) {
  40. >                if (owner) 
  41. >                   delete px;
  42. >                owner = r.owner; 
  43. >                px = r.release();
  44. >             }
  45. >             return *this;
  46. >          }
  47. >       ~auto_ptr()           { if (owner) delete px; }
  48. >       X& operator*()  const { return *px; }
  49. >       X* operator->() const { return px; }
  50. >       X* get()        const { return px; }
  51. >       X* release()    const { owner = 0; return px; }
  52. >    };
  53.  
  54. If I don't misunderstand the above implementation, it does much more
  55. than just allow auto_ptrs to be returned from functions.  It also
  56. allows auto_ptrs to be dereferenced after the ownership of the pointee
  57. has been transferred to another auto_ptr.
  58.  
  59. Is this intentional?  Did the committee explicitly decide to allow
  60. dereferencing of non-owning auto_ptrs?  If so, what is the rationale behind
  61. this? 
  62.  
  63. It seems to me that after the ownership has been transferred, the
  64. no-longer-owning auto_ptr points to an object of undetermined ownership
  65. and lifetime. In other words, its behavior is in no way different from
  66. a built-in pointer.
  67.  
  68. The old (April 95 DWP) definition of auto_ptr at least guaranteed that if an
  69. auto_ptr was pointing at something, it was pointing at a unique and alive
  70. object.  Now, we don't even have a way to query if this is the case. 
  71.  
  72. This is a terrible state of affairs, especially when we realize
  73. auto_ptr is the *only* smart pointer we have in the standard library.
  74. In my opinion, it should be renamed to zombie_ptr if it is to keep its
  75. new semantics.
  76.  
  77. - Wil
  78. ---
  79. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  80. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  81. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  82. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  83. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  84.